package com.ruoyi.puyu.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.puyu.domain.PuyuIntegral;
import com.ruoyi.puyu.service.IPuyuIntegralService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 积分明细Controller
 * 
 * @author wusongwen
 * @date 2024-12-18
 */
@RestController
@RequestMapping("/puyu/integral")
public class PuyuIntegralController extends BaseController
{
    @Autowired
    private IPuyuIntegralService puyuIntegralService;

    /**
     * 查询积分明细列表
     */
    @PreAuthorize("@ss.hasPermi('puyu:integral:list')")
    @GetMapping("/list")
    public TableDataInfo list(PuyuIntegral puyuIntegral)
    {
        if (SecurityUtils.hasRole(UserConstants.ROLE_MANAGER)
                || SecurityUtils.hasRole(UserConstants.ROLE_ADMIN)) {
            puyuIntegral.setUserId(null);
        } else if (SecurityUtils.hasRole(UserConstants.ROLE_STUDENT)) {
            puyuIntegral.setUserId(SecurityUtils.getUserId());
            puyuIntegral.setDelFlag("0");
        } else if (SecurityUtils.hasRole(UserConstants.ROLE_COACH)) {
            puyuIntegral.setCreateBy(SecurityUtils.getUsername());
            puyuIntegral.setDelFlag("0");
        } else if (SecurityUtils.hasRole(UserConstants.ROLE_PARENT)) {
            puyuIntegral.setUserId(null);
            puyuIntegral.setDelFlag("0");
            puyuIntegral.getParams().put("parent", SecurityUtils.getUserId());
        }
        startPage();
        List<PuyuIntegral> list = puyuIntegralService.selectPuyuIntegralList(puyuIntegral);
        return getDataTable(list);
    }

    /**
     * 导出积分明细列表
     */
    @PreAuthorize("@ss.hasPermi('puyu:integral:export')")
    @Log(title = "积分明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PuyuIntegral puyuIntegral)
    {
        List<PuyuIntegral> list = puyuIntegralService.selectPuyuIntegralList(puyuIntegral);
        ExcelUtil<PuyuIntegral> util = new ExcelUtil<PuyuIntegral>(PuyuIntegral.class);
        util.exportExcel(response, list, "积分明细数据");
    }

    /**
     * 获取积分明细详细信息
     */
    @PreAuthorize("@ss.hasPermi('puyu:integral:query')")
    @GetMapping(value = "/{integralId}")
    public AjaxResult getInfo(@PathVariable("integralId") Long integralId)
    {
        return success(puyuIntegralService.selectPuyuIntegralByIntegralId(integralId));
    }

    /**
     * 新增积分明细
     */
    @PreAuthorize("@ss.hasPermi('puyu:integral:add')")
    @Log(title = "积分明细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PuyuIntegral puyuIntegral)
    {
        return toAjax(puyuIntegralService.insertPuyuIntegral(puyuIntegral));
    }

    /**
     * 修改积分明细
     */
    @PreAuthorize("@ss.hasPermi('puyu:integral:edit')")
    @Log(title = "积分明细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PuyuIntegral puyuIntegral)
    {
        return toAjax(puyuIntegralService.updatePuyuIntegral(puyuIntegral));
    }

    /**
     * 删除积分明细
     */
    @PreAuthorize("@ss.hasPermi('puyu:integral:remove')")
    @Log(title = "积分明细", businessType = BusinessType.DELETE)
	@DeleteMapping("/{integralIds}")
    public AjaxResult remove(@PathVariable Long[] integralIds)
    {
        return toAjax(puyuIntegralService.deletePuyuIntegralByIntegralIds(integralIds));
    }
}
